const router = require("express")();
const fm = require("../core/FileManager");
const um = require("../core/UserManager");
const dm = require("../core/DataManager");

router.post("/cat-img", um.middle_checker, um.middle_loadUser, fm.getLoadPostFileFunction("image"), (req, res) => {
    let info = req.upload_info;
    if (info.err) {
        res.json(RES_CHECKER({ success: false, err: info.err }));
    } else {
        let cid = req.headers['x-cat-id'];
        if (isNaN(cid)) return res.json(RES_CHECKER({ success: false, err: `无效的猫咪ID: ${cid}` }));
        dm.addCatImg(info.f, info.ft, req.user.uid, cid, "", DM_RES(res));
    }
});

router.post("/thread-img", um.middle_checker, um.middle_loadUser, fm.getLoadPostFileFunction("image"), (req, res) => {
    let info = req.upload_info;
    if (info.err) {
        res.json(RES_CHECKER({ success: false, err: info.err }));
    } else {
        let lore = decodeURIComponent(req.headers['x-lore']);
        let tid = req.headers['x-tid'] || null;
        dm.addThreadImg(info.f, info.ft, req.user.uid, tid, lore, DM_RES(res));
    }
});

router.post('/user-img', um.middle_checker, um.middle_loadUser, fm.getLoadPostFileFunction("image"), (req, res) => {
    return res.end();//暂停自定义头像
    let info = req.upload_info;
    if (info.err) {
        res.json(RES_CHECKER({ success: false, err: info.err }));
    } else {
        dm.setUserSketch(req.user.uid, info.f, DM_RES(res));
    }
});

router.post('/thread-img-tid', um.middle_checker, um.middle_loadUser, (req, res) => {//TODO 无法编辑
    if (!req.body || !req.body.imgs) res.json({ success: true });
    const imgs = (req.body.imgs).split(',');
    const uid = req.user.uid;
    const tid = req.body.tid;
    if (!Array.isArray(imgs)) return res.json(RES_CHECKER({ success: false, err: 'Not Array' }));
    if (!tid) return res.json(RES_CHECKER({ success: false, err: 'No Tid' }));
    let tasks = imgs.length;
    let hasErr = false;
    const finish = err => {
        hasErr = hasErr || err;
        if (--tasks > 0) return;
        res.json(RES_CHECKER({ success: !hasErr, result: imgs }))
    };
    imgs.forEach((uuid, i) => dm.setThreadImgTid(uuid, uid, tid, err => {
        imgs[i] = err;
        finish(err);
    }));
});


router.post("/threads", um.middle_checker, um.middle_loadUser, async (req, res) => {
    let uid = req.user.uid;
    let tid;
    let title;
    let data = [];
    let tags;
    let back = err => {
        if (err) return res.json(RES_CHECKER({ success: false, err: err }));
        dm.uploadThread(tid, uid, title, data, (err0, result) => {
            if (err0) return res.json(RES_CHECKER({ success: false, err: err0 }));
            if (tags) {
                dm.setThreadTag(result, tags, (err1, result1) => {
                    if (err1) return res.json(RES_CHECKER({ success: true, err: err1, result: result }));
                    return res.json(RES_CHECKER({ success: true, result: result, tags: result1 }));
                });
            } else {
                return res.json(RES_CHECKER({ success: true, result: result }));
            }
        });
    };
    title = req.body.title;
    data = req.body.content;
    tid = req.body.tid;
    tags = req.body.tags;
    if (!title) return back('无标题');
    if (!data) return back('无内容');
    if (data.length > 4294967295) return back("内容过大");
    if (tags) {
        tags = tags.split(",");
    }
    if (tid) {
        dm.getThreadInfo(tid, (err, result) => {
            if (err) return back(err);
            if (!result) return back(`帖子 ${tid}不存在`);
            if (result.aj != uid || um.permissionCheck(true, req.user, 'edit_other_thread')) return back('没有编辑权限');
            back();
        });
    } else back();
});

router.post("/notice", um.middle_checker, um.middle_loadUser, (req, res) => {

});


//模块导出
module.exports = router;